package pcgen.android;

import org.apache.commons.lang.Validate;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.preference.PreferenceManager;

public class DefaultSharedPreferences 
{
	public static void commit(Context context)
	{
		Validate.notNull(context);
		
		try
		{
			SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
			SharedPreferences.Editor editor = preferences.edit();
			editor.commit();
		}
		catch (Throwable tr)
		{
			Logger.e(TAG, "commit", tr);
		}
	}
	
	public static SharedPreferences retrieve(Context context)
	{
		Validate.notNull(context);
		
		try
		{
			SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
			if (context instanceof OnSharedPreferenceChangeListener)
				preferences.registerOnSharedPreferenceChangeListener((OnSharedPreferenceChangeListener)context);
			
			return preferences;
		}
		catch (Throwable tr)
		{
			Logger.e(TAG, "retrieve", tr);
		}
		
		return null;
	}
	
	private static final String TAG = DefaultSharedPreferences.class.getSimpleName();
}
